<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/9/8
 * Time: 11:15
 */

use yii\helpers\Url;

$web = Url::base();
?>
<script>
    var card_id = "<?=$card_id?>";
    function activateAllVoucher() {
        var data = {};
        data.card_id = card_id;
        loading.show();
        requestJson("activate-card-voucher", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                getCardVoucher(card_id);
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }
    function exportVoucher() {
        var data = {};
        data.card_id = card_id;
        var result = requestJson("export-voucher-by-cardid-handler", data);
        if (result.status == 0) {
            var url = "<?= $web ?>/" + result.data;
//            $("#export-excel-download").attr("href", url);
            window.open(url);
        } else {
            alert(result.message);
        }
    }
    function activateVoucher(id) {
        var data = {};
        data.id = id;
        loading.show();
        requestJson("activate-voucher", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                getCardVoucher(card_id);
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }
    function invalidateVoucher(id) {
        var data = {};
        data.id = id;
        loading.show();
        requestJson("invalidate-voucher", data, true, function (response) {
            loading.hide();
            if (response.status == 0) {
                getCardVoucher(card_id);
            } else {
                $("#hint_message").text(response.message);
                $("#hintMessageModal").modal('show');
            }
        });
    }
</script>
<div class="form-group">
    <button class="btn btn-link btn-xs" type="button"
            onclick="activateAllVoucher()">
        全部激活
    </button>


    <button class="btn btn-link btn-xs" type="button"
            onclick="exportVoucher()">
        导出
    </button>
</div>
<table class="table table-bordered table-hover table-condensed">
    <thead class="nowrap">
    <tr>
        <th>单证号</th>
        <th>状态</th>
        <th>昵称</th>
        <th>手机号</th>
        <th>使用时间</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <?php
    if ($list != null && count($list) > 0) {
        for ($i = 0; $i < count($list); $i++) {
            ?>
            <tr>
                <td><?= $list[$i]['voucher'] ?></td>
                <td>
                    <?php
                    switch ($list[$i]['status']) {
                        case 0:
                            echo '<label class="label label-default">未使用</label>';
                            break;
                        case 1:
                        case 6:
                            echo '<label class="label label-info">已绑定</label>';
                            break;
                        case 2:
                            echo '<label class="label label-success">已激活</label>';
                            break;
                        case 3:
                            echo '<label class="label label-danger">已使用</label>';
                            break;
                        case 4:
                            echo '<label class="label label-warning">失效</label>';
                            break;
                        case 5:
                            echo '<label class="label label-warning">过期</label>';
                            break;
                    }
                    ?>
                </td>
                <td>
                    <?= $list[$i]['name'] ?>
                </td>
                <td>
                    <?= $list[$i]['phone'] ?>
                </td>
                <td>
                    <?= $list[$i]['create_datetime'] ?>
                </td>
                <td>
                    <?php
                    if ($list[$i]['status'] == 0 || $list[$i]['status'] == 1 || $list[$i]['status'] == 6) {
                        ?>
                        <button class="btn btn-link btn-xs" type="button"
                                onclick="activateVoucher('<?= $list[$i]['id'] ?>')">
                            激活
                        </button>
                        <?php
                    }
                    ?>

                    <?php
                    if ($current_user_id == '0' && ($list[$i]['status'] == 0 || $list[$i]['status'] == 1 || $list[$i]['status'] == 2 || $list[$i]['status'] == 6)) {
                        ?>
                        <button class="btn btn-link btn-xs" type="button"
                                onclick="invalidateVoucher('<?= $list[$i]['id'] ?>')">
                            失效
                        </button>
                        <?php
                    }
                    ?>
                </td>
            </tr>
            <?php
        }
    }
    ?>
    </tbody>
</table>
